package com.example.swordjava;


public class Test23 {

    public static void main(String[] args) {

        int[] a = {2, 3, 1, 0, 2, 5, 3};
    }

    class ListNode {
        ListNode next = null;
        int val;

        public ListNode(int val) {
            this.val = val;
        }
    }

    /**
     * 给一个长度为n链表，若其中包含环，请找出该链表的环的入口结点，否则，返回null。
     */

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        ListNode fast = pHead;
        ListNode slow = pHead;

        while (fast.next != null && fast != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (slow == fast) {
                fast = pHead;
                while (fast != slow) {
                    fast = fast.next;
                    slow = slow.next;
                }
                return slow;
            }
        }
        return null;
    }

}
